// 购物车数据action
import { get, post } from '@common/ajax';
import urls from '@common/url';
import logger from '@common/logger';
import { SHOP_MODE_TYPE, SHOP_COLUMN_LOCATION } from '@constants/shop';
import {
    GET_SHOP_STATUS_INFO,
    GET_SHOP_INDEX_DATA,
    GET_SHOP_COLUMN_DATA,
    GET_SHOP_SHELF_DATA,
    GET_SHOP_INDEX_GROUPS,
    GET_SHOP_INDEX_LIBRARY_INFO,
    GET_SHOP_STORE_SHELF_DATA,
    SET_SHOP_INDEX_SHELF_PARAM,
    SET_SHOP_INDEX_STORE_PARAM,
    SET_SHOP_INDEX_ADDRESS_BACKUP,
    GET_SHOP_STORE_LIST_DATA,
} from '@store/mutationType';
import { ClearTip, TipLoading, Tip } from '@common/tip';
import { dealStoreListData } from '@biztools/shop';
const shop = {
    state: {
        // 商城开通状态
        isOpenShop: false,
        shopInfoData: null,
        libraryInfo: {},
        shopIndexLibraryInfo: null,
        shopIndexColumnData: {}, // 栏目信息
        shopIndexGroups: [], // 分组信息
        shopShelfData: [], // 货架信息
        shopStoreList: [], // 门店列表
        shopStoreShelfData: [], // 门店模式下，货架数据
        // 分页获取相关参数
        gruopId: '',
        shelfPageNumber: 1,
        shelfPageSize: 10,
        storePageNumber: 1,
        storePageSize: 10,
        addressInfoBackup: {},
    },
    mutations: {
        GET_SHOP_STATUS_INFO: (state, isOpenShop) => {
            Object.assign(state, { isOpenShop });
        },
        GET_SHOP_INDEX_DATA: (state, shopInfoData) => {
            Object.assign(state, { shopInfoData });
        },
        GET_SHOP_INDEX_GROUPS: (state, shopIndexGroups) => {
            Object.assign(state, { shopIndexGroups });
        },
        GET_SHOP_COLUMN_DATA: (state, shopIndexColumnData) => {
            Object.assign(state, { shopIndexColumnData });
        },
        GET_SHOP_SHELF_DATA: (state, shopShelfData) => {
            Object.assign(state, { shopShelfData });
        },
        GET_SHOP_STORE_SHELF_DATA: (state, shopStoreShelfData) => {
            Object.assign(state, { shopStoreShelfData });
        },
        SET_SHOP_INDEX_SHELF_PARAM: (state, { shelfPageNumber, groupId }) => {
            console.log('shelfPageNumber', shelfPageNumber);
            Object.assign(state, { shelfPageNumber, groupId });
        },
        SET_SHOP_INDEX_STORE_PARAM: (state, { storePageNumber, groupId }) => {
            Object.assign(state, { storePageNumber, groupId });
        },
        SET_SHOP_INDEX_ADDRESS_BACKUP: (state, addressInfoBackup) => {
            Object.assign(state, { addressInfoBackup });
        },
        GET_SHOP_STORE_LIST_DATA: (state, shopStoreList) => {
            Object.assign(state, { shopStoreList });
        },
        GET_SHOP_INDEX_LIBRARY_INFO: (state, shopIndexLibraryInfo) => {
            Object.assign(state, { shopIndexLibraryInfo });
        },
    },
    actions: {
        // 获取商城状态
        getShopStatusInfo: async ({ commit }) => {
            const res = await get(urls.shopStatus);
            commit(GET_SHOP_STATUS_INFO, res.is_open);
        },
        // 获取商城首页信息
        getShopInfoData: async ({ commit, dispatch }) => {
            const res = await get(urls.shopIndex);
            logger.info('Shop => 获取到的首页数据', res);
            commit(GET_SHOP_INDEX_DATA, res);
        },
        // 获取首页分组信息
        getShopIndexGroups: async ({ commit, dispatch }, isMain) => {
            const res = await get(urls.shopGroup, { id: isMain ? 0 : -1 });
            logger.info('Shop => 获取到首页的分组数据', res);
            commit(GET_SHOP_INDEX_GROUPS, [{
                id: 0,
                name: '全部',
            }, ...res.groups]);
            logger.info('Shop => 执行获取首页数据', res.groups.length);
            logger.info('Shop => 执行获取首页数据', res);
            dispatch('getShopIndexShelf', 0);
        },
        // 匹配仓库信息,更具地址获取最近仓库的id
        getLibraryInfo: async ({ commit }, { addressInfo, isMain }) => {
            const info = await get(urls.libraryInfo, {
                lat: addressInfo.latitude,
                lng: addressInfo.longitude,
                city_code: addressInfo.city_code,
                // 是否总部优先
                hq_first: 1,
            });
            commit(SET_SHOP_INDEX_ADDRESS_BACKUP, addressInfo);
            logger.success('Shop => 获取的仓库信息为：', info);
            commit(GET_SHOP_INDEX_LIBRARY_INFO, { ...info, store_id: 0 });
        },
        // 获取门店模式，门店列表
        getStoreList: async ({ commit, state, dispatch }, groupId) => {
            TipLoading('加载中...');
            const { addressInfoBackup, storePageSize } = state;
            // const { shopStoreShelfData } = state;
            const param = {
                lat: addressInfoBackup.latitude,
                lng: addressInfoBackup.longitude,
                city_code: addressInfoBackup.city_code,
                page_no: 1,
                page_size: storePageSize,
            };
            const res = await get(urls.shopStoreList, param);
            const storeList = dealStoreListData(res.stores);
            logger.success('Shop => 获取的门店列表为：', storeList);
            commit(GET_SHOP_STORE_LIST_DATA, storeList);
            // 清除数据
            commit(GET_SHOP_STORE_SHELF_DATA, []);
            // 设置
            commit(SET_SHOP_INDEX_STORE_PARAM, { storePageNumber: 1, groupId, });
            dispatch('getStoreShelfData', storeList);
            ClearTip();
        },
        // 获取门店下一页
        getStoreNextList: async ({ commit, state, dispatch }) => {
            TipLoading('加载中...');
            const {  groupId, storePageNumber, storePageSize, addressInfoBackup, shopStoreList } = state;
            const pageNo = storePageNumber + 1;
            const param = {
                lat: addressInfoBackup.latitude,
                lng: addressInfoBackup.longitude,
                city_code: addressInfoBackup.city_code,
                page_no: pageNo,
                page_size: storePageSize,
            };
            const res = await get(urls.shopStoreList, param);
            commit(GET_SHOP_STORE_LIST_DATA, [...shopStoreList, res.stores]);
            commit(SET_SHOP_INDEX_STORE_PARAM, { storePageNumber: pageNo, groupId, });
            dispatch('getStoreShelfData', res.stores);
            ClearTip();
        },
        // 获取门店货架信息
        getStoreShelfData: async ({ commit, state }, storeList ) => {
            const { shopStoreShelfData, groupId } = state;
            let result = [];
            // 分门店获取栏目信息
            for (let i = 0; i < storeList.length; i++) {
                const param = {
                    id: storeList[i].id,
                    group_id: groupId,
                    query: '',
                    page_no: 1,
                    page_size: 3,
                };
                const info = await get(urls.shopShelf, param);
                result.push(info);
            }
            let _shopStoreShelfData = [...shopStoreShelfData, ...result];
            logger.success('Shop => 门店货架数据：', _shopStoreShelfData);
            commit(GET_SHOP_STORE_SHELF_DATA, _shopStoreShelfData);
        },
        // 获取商城栏目信息
        getShopIndexColumnData: async ({ commit }) => {
            const param = {
                id: 0,
                location: SHOP_COLUMN_LOCATION.index,
            };
            const res = await get(urls.shopColumn, param);
            logger.success('Shop => 获取的首页栏目信息', res);
            commit(GET_SHOP_COLUMN_DATA, res);
        },
        // 获取首页货架数据
        getIndexShelfData: async ({ commit, state }, groupId) => {
            const { shelfPageSize } = state;
            const param = {
                id: 0,
                group_id: groupId,
                query: '',
                page_no: 1,
                page_size: shelfPageSize,
            };
            const res = await get(urls.shopShelf, param);
            logger.success('Shop => 获取到的货架信息为', res.prods);
            commit(SET_SHOP_INDEX_SHELF_PARAM, { shelfPageNumber: 1, groupId, });
            commit(GET_SHOP_SHELF_DATA, res.prods);
        },
        // 获取首页货架数据下一页
        getIndexShelfNextPageData: async ({ commit, state }) => {
            TipLoading('加载中...');
            const {  groupId, shelfPageNumber, shelfPageSize, shopShelfData } = state;
            const pageNo = shelfPageNumber + 1;
            const param = {
                id: 0,
                group_id: groupId,
                query: '',
                page_no: pageNo,
                page_size: shelfPageSize,
            };
            const res = await get(urls.shopShelf, param);
            const _result = [...shopShelfData, ...res.prods];
            commit(SET_SHOP_INDEX_SHELF_PARAM, { shelfPageNumber: pageNo, groupId, });
            commit(GET_SHOP_SHELF_DATA, _result);
            ClearTip();
            res.prods.length === 0 && Tip('已加载全部数据');
        },
        // 获取新类型数据
        getShopIndexShelf: async ({ dispatch, state }, groupId) => {
            const { shopInfoData } = state;
            logger.warn('当前门店信息为：', shopInfoData);
            if (shopInfoData.operation_mode === SHOP_MODE_TYPE.brand) {
                // 品牌
                dispatch('getIndexShelfData', groupId);
            }
            if (shopInfoData.operation_mode === SHOP_MODE_TYPE.store) {
                // 门店
                dispatch('getStoreList', groupId);
            }
        },
        // 获取下一页内容
        getShopIndexShelfNext: async ({ state, dispatch }) => {
            const { shopInfoData } = state;
            if (shopInfoData.operation_mode === SHOP_MODE_TYPE.brand) {
                // 品牌
                dispatch('getIndexShelfNextPageData');
            }
            if (shopInfoData.operation_mode === SHOP_MODE_TYPE.store) {
                // 门店
                dispatch('getStoreNextList');
            }
        },
        getSpecialStoreInfoById: async ({ state }, id) => {
            const { shopStoreList } = state;
            const info = shopStoreList.filter(item => item.id === id)[0];
            return info;
        },
    },
};

export default shop;
